import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/wroks/order_item_info.dart';
import 'package:app/src/http/bean/wroks/product_bean.dart';
import 'package:app/src/http/bean/wroks/special_clauses.dart';
import 'package:app/src/ui/route/helper/route_helper.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_card.dart';
import 'package:app/src/utils/a_combined_utils.dart';
import 'package:app/src/utils/image_path_utils.dart';

import '../../../../../http/bean/wroks/carfreightrates_bean.dart';
import '../../../../../http/bean/wroks/workreview_bean.dart';
import '../../../../app_ui.dart';

///加班审核，加班审批
class UiWorkReviewItemInfo {
  static Widget build(
      BuildContext context, List<List<WorkOvertimeApproval>>? listDatas,
      {String? title}) {
    if (StringUtils.isEmpty(listDatas)) {
      return AppUi.getEmptyView();
    }
    List<Widget> children = [];
    if (StringUtils.isEmpty(title)) {
      title = AppStrings.of(context).Workovertimeapproval; //加班审批
    }
    children.add(
        UiBase.title(context, title, bottom: AppEdgeInsets.margin_bottom)); //标题
    children.add(_table(context, listDatas!));
    var column = UiBase.column(context, children);
    var card = UiCard.build(context, column);
    return card;
  }

  ///fixme Table表格里的行TabRow;每一行的表格数量必须相等。不然报错。
  ///fixme table嵌套table实现单元格合并。

  //表格线条颜色
  static Color _BorderColor = Colors.black12;

  //表格线条宽度
  static double _width = 1;

  ///背景色表格标题
  static Color _backgroundColorWithTableTitle = Colors.black12;
  static Color _backgroundColorWithWhite = Colors.white;
  static Color _backgroundColorWithGrey = ColorUtils.parseColor("#F7F6F3");
  static StateSetter? _setState = null;

  static Widget _table(
      BuildContext context, List<List<WorkOvertimeApproval>> listDatas) {
    if (listDatas.length <= 0) {
      return LibUi.getEmptyView();
    }
    //fixme 表格
    return StatefulBuilder(builder: (context, setState) {
      _setState = setState;
      //可以水平滑动的表格
      var scrolltable = _scrolltable(context, listDatas);
      //水平滑动效果
      var singleChildScrollView = SingleChildScrollView(
          child: scrolltable, scrollDirection: Axis.horizontal);
      return Center(
        child: Container(
          child: singleChildScrollView,
          //width: 800,
        ),
      );
    });
  }

  ///水平滑动表格
  static Widget _scrolltable(
      BuildContext context, List<List<WorkOvertimeApproval>> listDatas) {
    var children = <TableRow>[];
    //第一行标题栏
    var tableRow = TableRow(
        decoration: BoxDecoration(color: _backgroundColorWithTableTitle),
        children: [
          //fixme 内部嵌套表格(标题)
          _title(context),
          _text(AppStrings.of(context).Flightdate), //班次日期
          _text(AppStrings.of(context).ShiftName), //班次名称
          _text(AppStrings.of(context).isInClass), //调班
          _title2(context)
        ]);
    children.add(tableRow);
    //第二行，内容行。
    _index = 0;
    _index2 = 0;
    for (int i = 0; i < listDatas.length; i++) {
      var datas = listDatas[i];
      var data = datas[0];
      var tableRow = TableRow(
          // decoration: BoxDecoration(color: _getColor(_index + 1)),
          decoration: BoxDecoration(color: _getColor(i)),
          children: [
            ///fixme 内部嵌套表格(内容)
            _content(context, datas),
            //班次日期
            _text("${data.Flightdate}"),
            //班次名称
            _text("${data.ShiftName}"),
            //调班
            _checkbox(data.isInClass, isEnable: data.isInClassEnable,
                onChanged: (bool) {
              data.isInClass = !data.isInClass;
              datas.forEach((element) {
                element.isInClass = data.isInClass; //同步
              });
              //_setState?.call(() {});
            }),
            _content2(context, datas),
          ]);
      children.add(tableRow);
    }
    var table = Table(
      border: TableBorder.all(
          color: _BorderColor, width: _width, style: BorderStyle.solid),
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
      columnWidths: {
        0: FixedColumnWidth(1350),
        1: FixedColumnWidth(150),
        2: FixedColumnWidth(100),
        3: FixedColumnWidth(100),
        4: FixedColumnWidth(100),
      },
      children: children,
    );
    return table;
  }

  ///内部嵌套表格(标题)
  static Widget _title(BuildContext context) {
    var table = Table(
      border: TableBorder.symmetric(
        inside: BorderSide(
            color: _BorderColor, width: _width, style: BorderStyle.solid),
      ),
      children: [
        TableRow(children: [
          _text(AppStrings.of(context).TheSerial), //序号
          _text(AppStrings.of(context).TheName), //姓名
          _text(AppStrings.of(context).FactoryName3), //工厂
          _text(AppStrings.of(context).ClassName), //班组
          _text(AppStrings.of(context).OvertimeHours3), //当前加班时长（分钟）
          _text(AppStrings.of(context).Overtime), //当前加班时间
          _text(AppStrings.of(context).Attendancetime), //考勤时间
          _text(AppStrings.of(context).Flighttime), //班次时间
        ]),
      ],
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
      columnWidths: {
        0: FixedColumnWidth(50),
        1: FixedColumnWidth(100),
        2: FixedColumnWidth(200),
        3: FixedColumnWidth(200),
        4: FixedColumnWidth(200),
        5: FixedColumnWidth(200),
        6: FixedColumnWidth(200),
        7: FixedColumnWidth(200),
      },
    );
    return table;
  }

  ///内部嵌套表格(标题)
  static Widget _title2(BuildContext context) {
    var table = Table(
      border: TableBorder.symmetric(
        inside: BorderSide(
            color: _BorderColor, width: _width, style: BorderStyle.solid),
      ),
      children: [
        TableRow(children: [
          _text(AppStrings.of(context).isOvertime), //"是否加班"
        ]),
      ],
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
      columnWidths: {
        0: FixedColumnWidth(100),
      },
    );
    return table;
  }

  //fixme 条目颜色背景
  static Color _getColor(int i) {
    Color _color = _backgroundColorWithWhite;
    //print("=============$i ${i%2}");
    if (i % 2 == 0) {
      _color = _backgroundColorWithGrey;
    }
    return _color;
  }

  ///下标；这里主要作为总序号使用。
  static int _index = 0;

  ///内部嵌套表格(内容)
  ///datas 数据
  static Widget _content(
      BuildContext context, List<WorkOvertimeApproval> datas) {
    var children = <TableRow>[];
    for (int i = 0; i < datas.length; i++) {
      var data = datas[i];

      _index++; //fixme 序号加一
      var tableTow = TableRow(
          //decoration: BoxDecoration(color: _getColor(_index)),
          children: [
            //序号
            _text("${_index}"),
            //姓名
            _text("${data.TheName}"),
            //工厂
            _text("${data.FactoryName}"),
            //班组
            _text("${data.ClassName}"),
            //当前加班时长（分钟）
            _text("${data.OvertimeHours}"),
            //当前加班时间
            _text("${data.Overtime}"),
            //考勤时间
            _text("${data.Attendancetime}"),
            //班次时间
            _text("${data.Flighttime}"),
          ]);
      children.add(tableTow);
    }
    var table = Table(
      border: TableBorder.symmetric(
          inside: BorderSide(
              color: _BorderColor, width: _width, style: BorderStyle.solid),
          outside: BorderSide.none),
      children: children,
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
      columnWidths: {
        0: FixedColumnWidth(50),
        1: FixedColumnWidth(100),
        2: FixedColumnWidth(200),
        3: FixedColumnWidth(200),
        4: FixedColumnWidth(200),
        5: FixedColumnWidth(200),
        6: FixedColumnWidth(200),
        7: FixedColumnWidth(200),
      },
    );
    return table;
  }

  static int _index2 = 0;

  ///内部嵌套表格(内容)
  ///datas 数据
  static Widget _content2(
      BuildContext context, List<WorkOvertimeApproval> datas) {
    var children = <TableRow>[];
    for (int i = 0; i < datas.length; i++) {
      var data = datas[i];
      _index2++;
      var tableTow = TableRow(
          //decoration: BoxDecoration(color: _getColor(_index2)),
          children: [
            //是否加班
            _checkbox(data.isOvertime, onChanged: (bool) {
              data.isOvertime = !data.isOvertime;
              // _setState?.call(() {
              //   data.isOvertime = !data.isOvertime;
              // });
            })
          ]);
      children.add(tableTow);
    }
    var table = Table(
      border: TableBorder.symmetric(
          inside: BorderSide(
              color: _BorderColor, width: _width, style: BorderStyle.solid),
          outside: BorderSide.none),
      children: children,
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
      columnWidths: {
        0: FixedColumnWidth(100),
      },
    );
    return table;
  }

  //文本
  static Widget _text(String? text) {
    // return Center(
    //   child: Text(text ?? ""),
    // );
    return Center(
      child: ConstrainedBox(
        constraints: BoxConstraints(
          minHeight: 36.0, //最小高度
        ),
        child: Center(
          child: Text(text ?? ""),
        ),
      ),
    );
  }

  //复选框
  //isChecked 是否选中
  //isEnable 是否可操作
  //onChanged 选中回调
  static Widget _checkbox(bool isChecked,
      {bool isEnable = true, Function(bool)? onChanged}) {
    Color activeColor = Colors.blueAccent;
    Color inactiveColor = Colors.black;
    if (!isEnable) {
      //不可用，颜色变暗。
      activeColor = Colors.black12;
      inactiveColor = Colors.black12;
    }
    return Container(
      alignment: Alignment.center,
      // height: 30,
      // width: 30,
      padding: EdgeInsets.zero,
      margin: EdgeInsets.zero,
      child: CircleCheckbox(
        isEnable: isEnable,
        value: isChecked,
        //value 是否选中
        width: 20,
        //width 宽度
        height: 20,
        //height 高度
        activeColor: activeColor,
        //activeColor 选中时背景颜色
        inactiveColor: inactiveColor,
        //inactiveColor 未选中时颜色
        style: CircleCheckboxStyle.tick_hollow,
        //circle_point 圆点样式
        //checked: , 自定义选中组件；fixme 优先级比以上属性设置要高。
        //unChecked: ,自定义未选中组件
        onChanged: (bool) {
          //选中状态改变时回调，如点击时。
          //选中状态改变时回调。
          //setState(() {});
          if (isEnable) {
            onChanged?.call(bool);
          }
        },
      ),
    );
  }
}
